package com.example.uit_management;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HeaderElement;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.ParseException;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.protocol.HTTP;

import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.Activity;
import android.os.StrictMode;
import android.util.Log;

/** Theo tác trên sever Student(Thông tin về sinh viên, thông tinh về tài khoảng)	 */
//@TargetApi(9)
//@SuppressLint({ "NewApi", "NewApi", "NewApi", "NewApi", "NewApi", "NewApi", "NewApi" })
public class JsonParser_StudentPass  {
//////////////////////////////////////////////////////////
//Các key trong Json
/**Mã sinh viên  */
static final String KEY_name = "name";
/**Địa chỉ sinh viên  */
static final String KEY_studentAddress = "studentAddress";
/**Mã số sinh viên */
static final String KEY_studentID = "studentID";
/**Tên lớp của sinh viên */
static final String KEY_classID = "classID";
/**Tên sinh viên */
static final String KEY_studentName = "studentName";
/**Giới tính sinh viên */
static final String KEY_studentGender = "studentGender";
/**Ngày tháng năm sinh*/
static final String KEY_studentBirthday = "studentBirthday";
/**Mật khẩu tài khoảng*/
static final String KEY_passWord = "passWord";
/**ID của khoa*/
static final String KEY_facultyID = "facultyID";
/**Tên khoa*/
static final String KEY_facultyName = "facultyName";

/**Hàm khỏi tạo  JsonParser_StudentPass*/
public JsonParser_StudentPass() {
	// TODO Auto-generated constructor stub
}

/**
Put Data check password(Mã sinh viên, password)
*/
public String Check_StudentPass(String studentID, String passWord){
	

	Log.e(JsonParser_StudentPass.class.toString(),"4.1");	
	HttpParams params = new BasicHttpParams(); 
	HttpConnectionParams.setConnectionTimeout(params, 15000);
	HttpConnectionParams.setSoTimeout(params, 20000);
	DefaultHttpClient hc=new DefaultHttpClient(params);
	Log.e(JsonParser_StudentPass.class.toString(),"4.2");
	ResponseHandler <String> res=new BasicResponseHandler();
	Log.e(JsonParser_StudentPass.class.toString(),"4.3");
	HttpPost postMethod=new HttpPost("http://uitstudentmanagement.appspot.com/student");
	Log.e(JsonParser_StudentPass.class.toString(),"4.4");
	List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
	Log.e(JsonParser_StudentPass.class.toString(),"4.5");
	//Mã số sinh viên, cũng là tên tài khoảng
	nameValuePairs.add(new BasicNameValuePair("studentID", studentID));
	//Mật khẩu tài khoảng
	nameValuePairs.add(new BasicNameValuePair("passWord", passWord));
	//oper=check để check tài khoảng
	nameValuePairs.add(new BasicNameValuePair("oper", "check"));
	Log.e(JsonParser_StudentPass.class.toString(),"4.6");
	try {
		Log.e(JsonParser_StudentPass.class.toString(),"4.7");
		postMethod.setEntity(new UrlEncodedFormEntity(nameValuePairs));
	} catch (UnsupportedEncodingException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}  
	try {
		Log.e(JsonParser_StudentPass.class.toString(),"4.8");
		// du lieu tra ve cho client
		String response=hc.execute(postMethod,res);
		Log.e(JsonParser_StudentPass.class.toString(),"4.9");
		return response;
	} catch (ClientProtocolException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	} catch (IOException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	return "false";
}
/**
Put Data change password(Mã sinh viên, pass hien tai, pass moi)
*/

public String Change_StudentPass(String studentID, String currentPassword, String newPassword){
	DefaultHttpClient hc=new DefaultHttpClient();
	ResponseHandler <String> res=new BasicResponseHandler();
	HttpPost postMethod=new HttpPost("http://uitstudentmanagement.appspot.com/student");
	List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
	//Mã số sinh viên, cũng là tên tài khoảng
	nameValuePairs.add(new BasicNameValuePair("studentID", studentID));
	//Mat khau hien tai
	nameValuePairs.add(new BasicNameValuePair("currentPassword", currentPassword));
	//Mat khau moi 
	nameValuePairs.add(new BasicNameValuePair("newPassword", newPassword));
	//oper=check để check tài khoảng
	nameValuePairs.add(new BasicNameValuePair("oper", "changePass"));
	try {
		postMethod.setEntity(new UrlEncodedFormEntity(nameValuePairs));
	} catch (UnsupportedEncodingException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}  
	try {
		// du lieu tra ve cho client
		String response=hc.execute(postMethod,res);
		return response;
	} catch (ClientProtocolException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	} catch (IOException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	return "false";
}
///////////////////////////////
public static String PrepareSendPostData_DetailsActivity(String studentID, String passWord) {

    //Create a new HttpClient and Post Header
HttpClient httpclient = new DefaultHttpClient();

HttpPost httppost = new HttpPost("http://uitstudentmanagement.appspot.com/student");

try {

List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(100);

nameValuePairs.add(new BasicNameValuePair("studentID", studentID));
nameValuePairs.add(new BasicNameValuePair("passWord", passWord));
nameValuePairs.add(new BasicNameValuePair("oper", "check"));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);

String responseBody = getResponseBody(response);

if (responseBody != null)
    return responseBody;
else
    return null;

} catch (ClientProtocolException e) {
Log.e("exception here", e.getMessage().toString());
return null;
} catch (IOException e) {
Log.e("exception here 2", e.getMessage().toString());
return null;
}

}

public static String getResponseBody(HttpResponse response) {

String response_text = null;
HttpEntity entity = null;
try {
entity = response.getEntity();
response_text = _getResponseBody(entity);
} catch (ParseException e) {
e.printStackTrace();
} catch (IOException e) {
if (entity != null) {
    try {
        entity.consumeContent();
    } catch (IOException e1) {
    }
}
}
return response_text;
}

public static String _getResponseBody(final HttpEntity entity) throws IOException, ParseException {

if (entity == null) {
throw new IllegalArgumentException("HTTP entity may not be null");
}

InputStream instream = entity.getContent();

if (instream == null) {
return "";
}

if (entity.getContentLength() > Integer.MAX_VALUE) {
throw new IllegalArgumentException(

"HTTP entity too large to be buffered in memory");
}

String charset = getContentCharSet(entity);

if (charset == null) {

charset = HTTP.DEFAULT_CONTENT_CHARSET;

}

Reader reader = new InputStreamReader(instream, charset);

StringBuilder buffer = new StringBuilder();

try {

char[] tmp = new char[1024];

int l;

while ((l = reader.read(tmp)) != -1) {

    buffer.append(tmp, 0, l);

}

} finally {

reader.close();

}

return buffer.toString();

}

public static String getContentCharSet(final HttpEntity entity) throws ParseException {

if (entity == null) {
throw new IllegalArgumentException("HTTP entity may not be null");
}

String charset = null;

if (entity.getContentType() != null) {

HeaderElement values[] = entity.getContentType().getElements();

if (values.length > 0) {

    NameValuePair param = values[0].getParameterByName("charset");

    if (param != null) {

        charset = param.getValue();

    }

}

}

return charset;

}
}
